/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hearts;

import java.awt.*;

import java.awt.event.*;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author TAI
 */
public class giaoDien extends JFrame{
	final private int xresolusion=800;//Chieu rong cua giao dien game
	final private int yresolusion=600;//Chieu cao cua giao dien game
	final private int cardWidth=100;//Do rong cua moi card
	final private int cardHeight=135;//Chieu cao cua moi card

	private JLabel[] p1,p2,p3,p4;//JLabel dung de hien thi danh sach card cua 4 player
                                     // p1 la human co gan su kien nhap chuot mlisten
                                     // p2,p3,p4 la computer

        private JLabel[] gameCard;//JLabel dung de hien thi card duoc player dua ra trong moi luot choi
	private player human,com1,com2,com3;//4 player
	private ArrayList<card> bonCard;//Trong 1 luot choi dung de luu card cua player dua ra ,sau moi luot choi se duoc clear de luu 1 luot moi
	private myMouseListener mlisten;//Tao su kien lang nghe nhap chuot

	private Container mainPanel ;//Dung de hien thi giao dien chinh cua game
	private JLabel note;//JLabel dung de chu thich
	private JButton mybutton;//JButton dung de pass left 3 card , newround , newgame
	private JLabel[] score;//Dung de hien thi diem cua 4 player
	private int showComCard=0;//Bien cho biet co xem bai cua computer .
                                  //1 thi duoc xem . Neu khong thi khong duoc xem
                                  //Ngoai ra dung de chuyen Background Cards cua computer
                                  //Co gia tri 0,2 la 2 background cua card
        private int bCheckB1=0,bCheckB2=0;//Bien bCheckB1 dung de kiem tra xem CheckBox xem bai da bat hay chua
                                           //Bien bCheckB2 dung de kiem tra xem CheckBox doi background da bat hay chua

	public giaoDien(final gameEngine ge){
		super("Java Heart game !");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);

		mlisten=new myMouseListener(ge.getSignal());
		score=new JLabel[4];

		this.human=ge.getPlayer(1);
		this.com1=ge.getPlayer(2);
		this.com2=ge.getPlayer(3);
		this.com3=ge.getPlayer(4);
		this.bonCard=ge.getBonCard();
		setVisible(true);
		setSize(800,600);

		mainPanel = getContentPane();
		mainPanel.setLayout(null);
                mainPanel.setBackground(Color.green);
		//tao thanh menu
		JMenu menuFile=new JMenu("Menu");
		JMenuItem itemSave=new JMenuItem("Reset");
		itemSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ge.getSignal()[1]=1;
			}
		});
		menuFile.add(itemSave);

                JCheckBox checkB2=new JCheckBox("Change Background Cards");
		checkB2.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange()==1){
                                    showComCard = 2;
                                    bCheckB2 = 1;
                                }else{
                                    if(bCheckB1 == 1)
                                        showComCard = 1;
                                    else
                                        showComCard = 0;

                                    bCheckB2 = 0;
                                }
				drawAllCard();
			}
		});

                menuFile.add(checkB2);

                JCheckBox checkB1=new JCheckBox("Show Computer Cards");
		checkB1.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
                                if(e.getStateChange()==1){
                                    showComCard = 1;
                                    bCheckB1 = 1;
                                }else{
                                    if(bCheckB2 == 1)
                                        showComCard = 2;
                                    else
                                        showComCard = 0;
                                    bCheckB1 = 0;
                                }
				drawAllCard();
			}
		});

                menuFile.add(checkB1);

                ButtonGroup group = new ButtonGroup();
                JMenu menuFile1 = new JMenu("Speed");
                JRadioButton radio1= new JRadioButton("Slow",false);
		radio1.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
                                if(e.getStateChange()==1){
                                    ge.updateDelay(5000);
                                }
			}
		});

                group.add(radio1);
                menuFile1.add(radio1);
                
                JRadioButton radio2= new JRadioButton("Normal",true);
		radio2.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
                                if(e.getStateChange()==1){
                                    ge.updateDelay(500);
                                }   
			}
		});

                group.add(radio2);
                menuFile1.add(radio2);
                JRadioButton radio3 = new JRadioButton("Fast",false);
		radio3.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
                                    ge.updateDelay(50);
			}
		});
                group.add(radio3);
                menuFile1.add(radio3);
		JMenuBar menubar=new JMenuBar();
		menubar.setLayout(new BorderLayout());
		menubar.add(menuFile,BorderLayout.WEST);
                menubar.add(menuFile1,BorderLayout.CENTER);
		menubar.setBounds(0, 0, 800, 30);
		menubar.setBorder(new EtchedBorder(EtchedBorder.RAISED));

		mainPanel.add(menubar);

		//tao nut, diem o giua
		Container ultilcontain=new Container();
		ultilcontain.setLayout(null);
		mybutton=new JButton();
		mybutton.setName("0");
		mybutton.setBounds(350, 330, 100, 20);
		mybutton.addMouseListener(mlisten);
		ultilcontain.add(mybutton);
		//add them tuy thix
		Font f=new Font(Font.SANS_SERIF, Font.TRUETYPE_FONT, 20);
		for (int i=0;i<=3;i++){
			score[i]=new JLabel();
			score[i].setFont(f);
			ultilcontain.add(score[i]);
		}
		score[0].setBounds(120, 470, 100, 30);
		score[1].setBounds(10, 40, 100, 30);
		score[2].setBounds(580, 7, 100, 30);
		score[3].setBounds(690, 460, 100, 30);
		ultilcontain.setBounds(0, 30, 800, 540	);
		mainPanel.add(ultilcontain);


		//tao note o cuoi
		note=new JLabel("This is the help !!!");
		JPanel pn=new JPanel();
		pn.add(note);
		pn.setBackground(Color.lightGray);
		pn.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
		pn.setBounds(0, 538, 800, 30);
		mainPanel.add(pn);
		//drawCards(null,1	);
		validate();
		//mainPanel.remove(1);
	}

	public void notice(String st){
		note.setText(st);
	}


	public void hideButton(){
		mybutton.setVisible(false);
	}

	public void showbutton(String st){
		mybutton.setText(st);
		mybutton.setVisible(true);
	}

	public void updateScore(){
		score[0].setText("Score: "+String.valueOf(human.getScore()));
		score[1].setText("Score: "+String.valueOf(com1.getScore()));
		score[2].setText("Score: "+String.valueOf(com2.getScore()));
		score[3].setText("Score: "+String.valueOf(com3.getScore()));
	}
	//load du lieu tu 1 player ra 13 label
	private JLabel[] loadCard(player player1,int showcard){
		JLabel[] kq=new JLabel[14];
		ImageIcon im;
		String path;
		for (int i=1;i<14;i++)
			if (player1.layCardTrongDanhSach(i)!=null)
			{
				if ((showcard == 1)||(player1 instanceof human))
					path="52card/"+player1.layCardTrongDanhSach(i).getID()+".jpg";
				else if(showcard == 2)
                                    path="52card/0-1.jpg";
                                else
                                    path="52card/0-2.jpg";
                                    
                                URL url = getClass().getResource(path);
				im=new ImageIcon(url);
				kq[i]=new JLabel(im);
				if (player1 instanceof human){
					kq[i].setName(String.valueOf(i));
					kq[i].addMouseListener(mlisten);
				}
				if (!player1.kiemTraCardChuaChoi(i))
					kq[i].setVisible(false);
			}
		return kq;
	}

	private JLabel[] loadCard(ArrayList<card> fourcard){
		JLabel[] kq=new JLabel[4];
		ImageIcon im;
		String path;
		for (int i=0;i<fourcard.size();i++)
		{
			path="52card/"+fourcard.get(i).getID()+".jpg";
                        URL url = getClass().getResource(path);
			im=new ImageIcon(url);
			kq[i]=new JLabel(im);
		}
		return kq;
	}
	//ve 13 la bai o 1 trong 4 vi tri
	private void drawCards(JLabel jls[],int position){
		final int khoangcach=20;
		switch (position){
		case 1:
			int xtoado=(xresolusion-khoangcach*12-cardWidth)/2;
			for (int i=1;i<14;i++)
			if (jls[i]!=null)
			{
				jls[i].setBounds(xtoado,398, 100, 135);
				xtoado+=khoangcach;
				mainPanel.add(jls[i],3);
			}
			break;
		case 3:
			int xtoado1=(xresolusion-khoangcach*12-cardWidth)/2;
			for (int i=1;i<14;i++)
				if (jls[i]!=null)
				{
				jls[i].setBounds(xtoado1,35, 100, 135);
				xtoado1+=khoangcach;
				mainPanel.add(jls[i],3);
			}
			break;
		case 2:
			int ytoado=(yresolusion-khoangcach*12-cardHeight)/3;
			for (int i=1;i<14;i++)
				if (jls[i]!=null){
				jls[i].setBounds(7,ytoado+30, 100, 135);
				ytoado+=khoangcach;
				mainPanel.add(jls[i],3);
			}
			break;
		case 4:
			int ytoado1=(yresolusion-khoangcach*12-cardHeight)/3;
			for (int i=1;i<14;i++)
				if (jls[i]!=null){
				jls[i].setBounds(xresolusion-cardWidth-15,ytoado1+30, 100, 135);
				ytoado1+=khoangcach;
				mainPanel.add(jls[i],3);
			}
			break;
		case 5:
			int xtoado2=(xresolusion-khoangcach*bonCard.size()-cardWidth)/2;
			for (int i=0;i<bonCard.size();i++){
				jls[i].setBounds(xtoado2, 220, 100, 135);
				xtoado2+=khoangcach;
				mainPanel.add(jls[i],3);
			}
			break;
		}
	}


	private void clear4play(){
		while (mainPanel.getComponentCount()>3)
		{
			mainPanel.remove(3);
		}
	}


	public void drawAllCard(){
		clear4play();
		p1=loadCard(human, 1);
		p2=loadCard(com1, showComCard);
		p3=loadCard(com2, showComCard);
		p4=loadCard(com3, showComCard);
		gameCard=loadCard(bonCard);
		drawCards(p1, 1);
		drawCards(p2, 2);
		drawCards(p3, 3);
		drawCards(p4, 4);
		drawCards(gameCard,5);
		mainPanel.repaint();
		updateScore();
	}



	public void sethightlight(int humanCardIndex){
		p1[humanCardIndex].setEnabled(false);
	}

	public void setnomal(int humanCardIndex){
		p1[humanCardIndex].setEnabled(true);
	}


}

